import {
  getHotAlbum,
  getAllAlbum,
  getAlbumDetail,
  getAlbumComment
} from '@/services/album-api'
import * as actionTypes from './constants'
const changeHotAlbumsAction = hotAlbums => ({
  type: actionTypes.CHANGE_HOT_ALBUM_ACTION,
  hotAlbums
})
export const getHotAlbumsAction = () => {
  return async dispatch => {
    const res = await getHotAlbum()
    dispatch(changeHotAlbumsAction(res.albums))
  }
}
const changeAllAlbumsAction = allAlbums => ({
  type: actionTypes.CHANGE_ALL_ALBUM_ACTION,
  allAlbums
})
const changeAllCountAction = allCount => ({
  type: actionTypes.CHANGE_ALL_COUNT_ACTION,
  allCount
})
export const getAllAlbumsAction = (area, offset) => {
  return async dispatch => {
    const res = await getAllAlbum(area,offset)
    dispatch(changeAllAlbumsAction(res.albums))
    dispatch(changeAllCountAction(res.total))
  }
}
const changeAlbumDetailAction = albumDetail => ({
  type: actionTypes.CHANGE_ALBUM_DETAIL_ACTION,
  albumDetail
})
const changeAlbumSongsAction = albumSongs => ({
  type: actionTypes.CHANGE_ALBUM_SONGS_ACTION,
  albumSongs
})
export const getAlbumDetailAction = id => {
  return async dispatch => {
    const res = await getAlbumDetail(id)
    dispatch(changeAlbumSongsAction(res.songs))
    dispatch(changeAlbumDetailAction(res.album))
  }
}
const changeAlbumCommentsAction = albumComments => ({
  type: actionTypes.CHANGE_ALBUM_COMMENTS_ACTION,
  albumComments
})
const changeAlbumCommentsCount = commentsCount => ({
  type: actionTypes.CHANGE_ALBUM_COMMENTS_COUNT_ACTION,
  commentsCount
})

export const getAlbumCommentsAction = (id,offset) => {
  return async dispatch => {
    const res = await getAlbumComment(id, offset)
    dispatch(changeAlbumCommentsCount(res.total))
    dispatch(changeAlbumCommentsAction(res.comments))
  }
}